#include "../common.h"

int main()
{
    std::vector<std::string> v;
    v.push_back(("GET /Youtube/login?user=SulleTilly HTTP/1.1"));
    v.push_back(("GET /Youtube/login?user=SulleTilly HTTP/1.1\r\n"));
    v.push_back(("GET /Youtube/login HTTP/1.1\r\n"));
    v.push_back(("GET /Youtube/login HTTP/1.0\n"));
    // std::regex e("(GET|POST|HEAD|PUT|DELETE) (.*)[^\\?] (HTTP/1\\.[01](?:[\n|\r\n]))");
    std::regex e("(GET|POST|HEAD|PUT|DELETE) ([^?]*)(?:\\?(.*))? (HTTP/1\\.[01])(?:\n|\r\n)?");
    // ([^?]*)              匹配一个或多个非问号字符
    // (?:\\?(.*))?         如果有问号,就提取问号后面的内容;如果没有问号,就不提取 (...)?表示提取1次或0次
    // (HTTP/1\\.[01])      要匹配.必须用\\. [01]表示在0和1当中取一个
    // (?:\n|\r\n)?         ?:表示只捕捉不不不提取
    auto it = v.begin();
    while (it != v.end())
    {
        std::smatch matches;

        bool ret = std::regex_match(*it, matches, e);
        if (ret == false)
        {
            perror("regex_match error");
            return -1;
        }

        std::string buffer;
        for (auto &e : matches)
        {
            if (e.length() == (*it).length())
                continue;
            if (e.str() == "")
            {
                // std::cout << "e.str() == 0, continue\n\n";
                continue;
            }
            buffer += e.str();
            buffer += " ";
        }
        std::cout << buffer.c_str() << std::endl;
        it++;
    }

    return 0;
}